﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace itau.nk.entidade
{
    public class Conversor
    {
        public static string ConverterParaString(object valor)
        {
            return valor != null ? valor.ToString().Trim() : null;
        }

        public static int ConverterParaInt(object valor)
        {
            return Convert.ToInt32(valor == null || valor is DBNull ? "0" : valor);
        }

        public static double ConverterParaDouble(object valor)
        {
            return Convert.ToDouble(valor == null || valor is DBNull ? "0" : valor);
        }

        public static DateTime ConverterParaDateTime(object valor)
        {
            DateTime retorno;

            if (valor != null)
            {
                if (DateTime.TryParse(valor.ToString(), out retorno))
                {
                    return retorno;
                }
            }

            return DateTime.MinValue;
        }

        public static Char ConverterParaChar(object valor)
        {
            if(valor != null)
            {
                return Convert.ToChar(valor == null || valor is DBNull ? '0' : valor);
            }

            return char.MinValue;
        }

        public static object VerificarDataNula(DateTime dateTime)
        {
            if (dateTime == DateTime.MinValue)
            {
                return null;
            }
            else
            {
                return dateTime;
            }
        }
    }
}
